﻿using GameToolkit;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(VolumeProfileClip)), CanEditMultipleObjects]
public class VolumeProfileClipEditor : Editor
{
    SerializedProperty m_Profile;

    Editor mProfileEditor;

    void GetEditor(Object obj)
    {
        if (obj == null)
        {
            GlobalUtil.Destroy(mProfileEditor);
            mProfileEditor = null;
        }
        else if (mProfileEditor == null || mProfileEditor.target != obj)
        {
            GlobalUtil.Destroy(mProfileEditor);
            mProfileEditor = obj == null ? null : Editor.CreateEditor(obj);
        }
    }

    private void OnEnable()
    {
        m_Profile = serializedObject.FindProperty("m_Profile");
    }

    private void OnDisable()
    {
        GetEditor(null);
    }

    public override void OnInspectorGUI()
    {
        base.OnInspectorGUI();
        GetEditor(m_Profile.hasMultipleDifferentValues ? null : m_Profile.objectReferenceValue);
        if(mProfileEditor != null)
        {
            EditorGUILayout.BeginVertical();
            mProfileEditor.OnInspectorGUI();
            EditorGUILayout.EndVertical();
        }
    }
}
